function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return (r[2]);
    return null; //返回参数值
}

var orderNum = getUrlParam("orderNum");
var gateway = getUrlParam("gateway");
var payerId = getUrlParam("payerId");
var goodsName = null;
var bindId = null;
var accessBindId = null;
var orderIp = null;
var phone = null;
var rememberCard = null;
var name = null;
var terminalId = 1212121221313;
var terminalType = "IMEI";
var time = 0;
var timer = null;

// 确定订单信息
mui(document.body).on('tap', '.confirm-btn', function (e) {
    h5Pay();
});

// 获取短信验证码
mui(document.body).on('tap', '.code-btn', function (e) {
    if (time > 0) {
        return;
    }
    var that = mui(this);
    that.button('loading');
    if (bindId == null || bindId == 'null') {
        $.get("/thmlfApi/avs/hlbh5/sendpaycode?phone=" + phone + "&gateway=" + gateway + "&orderNum=" + orderNum + "&_d=" + Date.parse(new Date()), function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                time = 60;
                timer = setInterval(times, 1000);
            }
        });
    } else {
        $.get("/thmlfApi/avs/hlbh5/sendbindpaycode?phone=" + phone + "&gateway=" + gateway + "&orderNum=" + orderNum + "&bindId=" + bindId + "&_d=" + Date.parse(new Date()), function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                time = 60;
                timer = setInterval(times, 1000);
            }
        });
    }
});

function times() {
    if (time > 0) {
        $("#sms_code_btn").text(time + 's');
        time--;
    } else {
        clearInterval(timer);
        time = 0;
        $("#sms_code_btn").text('获取');
    }
}

// 合利宝H5支付
function h5Pay() {
    mui('.confirm-btn').button('loading');
    $.ajax({
        url:"/thmlfApi/avs/hlbh5/quickpay?accessBindId=" + accessBindId + "&orderId=" + orderNum + "&_d=" + Date.parse(new Date()),
        success:function (r) {
            mui('.confirm-btn').button('reset');
            if (r.code == "200") {
                document.getElementById("from-hidden").innerHTML = r.result;
                document.E_FORM.submit();
            } else {
                mui.alert(r.message);
            }
        },
        error:function (e) {
            mui('.confirm-btn').button('reset');
            mui.toast("系统繁忙");
            console.error("request error",e);
        }
    });
}

// 确认支付
mui(document.body).on('tap', '.mui-btn-danger', function (e) {
    //	window.location.href = 'pay-success.html';
    var that = mui(this);
    that.button('loading');
    var validateCode = $("#sms_code_input").val().trim();
    if (validateCode == "" || validateCode == null) {
        mui.alert("短信验证码不能为空");
        that.button('reset');
        return false;
    }
    var params = {
        "validateCode": validateCode,
        "orderNum": orderNum,
        "orderIp": orderIp,
        "gateway": gateway,
        "rememberCard": $("#rememberCard").prop("checked") ? 1 : 0,
        "phone": phone,
        "holdName": name,
        "bindId": bindId,
        "accessBindId": accessBindId,
        "terminalType": terminalType,
        "terminalId": terminalId
    };
    $.ajax({
        url: "/thmlfApi/avs/hlbh5/confirmpayms" + "?_d=" + Date.parse(new Date()),
        type: "POST",
        data: JSON.stringify(params),
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                mui.alert("支付成功.");
                window.location.href = "./pay-success.html"
            }
        },
        error: function (e) {
            mui.alert("网络错误，请稍后再试");
            that.button('reset');
        }
    });
});

$(function () {
    document.getElementById("select-card").style.display = "block";
    $.getJSON("/thmlfApi/avs/pay/getpayerallbind?gateway=hlbh5" + "&payerId=" + payerId + "&_d=" + Date.parse(new Date()), null, function (r) {
        if (r.code == 200) {
            $.each(r.result, function (index, data) {
                var cardType = data.cardType == "X" ? "信用卡" : "借记卡";
                var item = '<div class="mui-input-row mui-radio mui-left bind-card-item" data-access-bind-id="' + data.accessBindId + '" data-bindid="' + data.bindId + '" data-phone="' + data.phone + '">' +
                    '<label>' + data.cardName + '&nbsp;&nbsp;' + cardType + '&nbsp;&nbsp;' + data.holdName + '&nbsp;&nbsp;' + '尾号' + data.lastFourNum + '</label>' +
                    '<input name="card" type="radio">' +
                    '</div>';
                $(".add-new-card").before(item);
            });
            bindChoose();
        }
    })
    $("#addBindCard").click(function () {
        bindId = null;
        window.location.href = "./card-manage.html";
        // document.getElementById("select-card").style.display = "none";
        // document.getElementById("add-card").style.display = "block";
    });
});

// 选择银行卡
function bindChoose() {
    $(".bind-card-item").click(function () {
        bindId = $(this).attr("data-bindid");
        phone = $(this).attr("data-phone");
        accessBindId = $(this).attr("data-access-bind-id");
    })
}